<template>
    <div class="shopcar-container"> 

        <div class="goods-list">
            <!-- 商品列表区域 -->
            <div class="mui-card" v-for="(item,index) in goodslist" :key="item.goodsId">
			
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<mt-switch v-model="$store.getters.getgoodsselected[item.goodsId]"
                        @change="selectedchange(item.goodsId,$store.getters.getgoodsselected[item.goodsId])"></mt-switch>
                        <img :src="item.image" alt="">
                        <div class="info">
                        <h1>{{ item.name }}</h1>
                        <p>
                            <span class="price">{{ item.price }}</span>
                            <numbox :initcount="$store.getters.getgoodscount[item.goodsId]" :goodsid="item.goodsId"></numbox>
                            <!--  如何从购物车中获取商品的数量 -->
                            <!-- 1.我们可以先创建一个空对象，然后循环购物车中所有的商品数据，把当前循环这条商品ID作为对象的属性名，count值作为对象的属性值，这样当吧所有的商品循环一次。就会得到一个对象:{ 89:2,90:1,84:3} -->
                            <a href="#" @click.prevent="remove(item.id,index)">删除</a>
                        </p>
                        </div>
					</div>
				</div>
				
			</div>
        </div>
        <!-- 结算区域 -->
        <div class="mui-card">
			
				<div class="mui-card-content">
					<div class="mui-card-content-inner jiesuan" >
						<div class="left">
                            <p>
                                总计（不含运费）
                            </p>
                            <p>
                                已勾选商品<span class="red">{{ $store.getters.getgoodscountandallprice.count }}</span>件，总价 <span class="red">￥{{ $store.getters.getgoodscountandallprice.allprice }}</span>元
                            </p>

                        </div>
                        <mt-button type='danger'>去结算</mt-button>
					</div>
                    
				</div>
				
			</div>
            <p>{{ $store.getters.getgoodsselected }}</p>
    </div>
</template>
<script>
import numbox from '../subcomponents/shopcar-numbox.vue'
export default {
    data(){
        return{
            goodslist:[]  //购物车所有商品的数据

        }

    },
    created(){
        this.getgoodslist()

    },
    methods:{
        getgoodslist(){
        this.$http.get('https://www.easy-mock.com/mock/5c05f6478da5aa5aaa8b6076/vue').then(result =>{
            console.log(result)
            if(result.status === 200){
                this.goodslist = result.body.data.hotGoods.slice(0,4)


            }

        })
        },
        remove(id,index){
            //把商品从store 中 根据 传过来的 id来删除，同时把组件goodlist中对应的那个商品通过index来删除
            this.goodslist.splice(index,1);
            this.$store.commit('removelocalstoragedata',id);
            

        },
        selectedchange(goodsId,val){
            //每当点击开关，把最新的快关状态同步到 store 中
            console.log(goodsId + '---' + val)
            this.$store.commit('updategoodsselected',{ goodsId,selected:val })

        }

    },
    components:{
        numbox
    }
    
}
</script>
<style lang="scss" scoped>
.shopcar-container{
    background-color: #eee;
    overflow:hidden;
    .goods-list{
        .mui-card-content-inner{
            display: flex;
            align-items: center;
        }
        img{
            width: 60px;
            height: 60px;
        }
        h1{
            font-size:13px;
            text-align: center;
        }
        .info{
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            .price{
                color: red;
                font-weight:bold;

            }
        }
    }
    .jiesuan{
        .red{
            color: red;
            font-weight: bold;
            font-size: 16px;
        }
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
}

</style>

